import java.sql.*;

public class queryList {
    public static void main(String[] args) {
        //加载驱动
        // 获取连接
        Connection conn=null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String URL="jdbc:mysql://127.0.0.1:3306/jdbc";
            String username="root";
            String password="123456";
            conn=DriverManager.getConnection(URL,username,password);
            //编写SQL语句
            String sql="select *from sysuser";
            //预处理SQL
             preparedStatement = conn.prepareStatement(sql);
            //如果有参数，设置参数
            //执行SQL语句
             resultSet = preparedStatement.executeQuery();
            //如果是查询就遍历结果//如果是增删改  判断是否成功
            while(resultSet.next()){
                int id =resultSet.getInt(1);
                String name=resultSet.getString(2);
                String pass=resultSet.getString(3);
                System.out.println(id+"----"+name+"---"+pass);
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                //关闭资源
                //NullPointException
                if(resultSet!=null) resultSet.close();
                if(conn!=null) conn.close();
                if(preparedStatement!=null)  preparedStatement.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //返回视图

    }
}
